<?php

namespace app\components;

use yii;

/**
 * 短信通 106 短信验证接口
 * url : http://apistore.baidu.com/apiworks/servicedetail/1018.html
 *
**/
class Sms extends  \yii\base\Widget
{

   
    /*  发送短信   */
    public function sendSms($phone, $code){
         if(empty($phone)){
                return 'phone is null';
         }
         if(empty($code)){
                return 'code is null';
         }

        $post_data['userid'] = 4332;
        $post_data['account'] = 'lmeeting';
        $post_data['password'] = '131liuhan';
        $post_data['content'] = sprintf('【Lmeeting】您的验证码: %s',$code);
        //多个手机号码用英文半角豆号‘,’分隔
        $post_data['mobile'] = $phone;
        $url = 'http://sms.kingtto.com:9999/sms.aspx?action=send';
        $o = '';
        foreach ($post_data as $k=>$v)
        {
            $o.="$k=".urlencode($v).'&'; 
        }
        $post_data = substr($o,0,-1);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果需要将结果直接返回到变量里，那加上这句。
        $res = curl_exec($ch);

         // $ch = curl_init();
         // $url = sprintf("%s?mobile=%s&content=%s", yii::$app->params['sms_url'],$phone , urlencode(sprintf('【Lmeeting】您的验证码: %s',$code)) );
       
         // $header = [sprintf("apikey: %s", yii::$app->params['apikey'])];
        
         // // 添加apikey到header
         // curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
         // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         // // 执行HTTP请求
         // curl_setopt($ch , CURLOPT_URL , $url);
         // $res = curl_exec($ch);
       
         return $res;
    }


    /*  发送短信   */
    public function sendSms2($phone, $content){
         if(empty($phone)){
                return 'phone is null';
         }
         if(empty($content)){
                return 'content is null';
         }

         $ch = curl_init();
         $url = sprintf("%s?mobile=%s&content=%s", yii::$app->params['sms_url'],$phone , urlencode(sprintf('【Lmeeting】 %s',$content)) );
 
         $header = [sprintf("apikey: %s", yii::$app->params['apikey'])];
 
         // 添加apikey到header
         curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         // 执行HTTP请求
         curl_setopt($ch , CURLOPT_URL , $url);
         $res = curl_exec($ch);
        
         return $res;
    }

    
    
}

?>
